package com.cleargroup.magic.cleara.c.command;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.text.TextUtils;

import com.cleargroup.magic.cleara.c.BatteryHelper;
import com.cleargroup.magic.cleara.c.RamUtil;


public class ProcessItem {
	//private static final String TAG = ProcessItem.class.getSimpleName();

	public int pid;
	public long ram;
	//public float cpu;
	//public float bat;

	public String pkgName;
	public String appName;
	public String iconUri;
	//public ApplicationInfo appInfo;
	public Context context;
	public boolean isChecked = true;

//    public long mLastAppCpuTime = 0;
//    public long mLastTotalCpuTime = 0;

//	public ProcessItem(Context context, RunningAppProcessInfo process, long totalCpu, long totalRam) {
//		this(context, process.pid, process.pkgList[0], totalCpu, totalRam);
//	}
	public ProcessItem(Context context, RunningAppProcessInfo process) {
		this(context, process.pid, process.pkgList[0]);
	}

	/*
	 * 06-0516:13:10.081E/AndroidRuntime(14199):FATALEXCEPTION:main 06-0516:13
	 * :10.081E/AndroidRuntime(14199):java.lang.NullPointerException 06-0516:13:
	 * 10.081E/AndroidRuntime(14199):atimoblife.toolbox.full.process.AProcess$ProcessItem.(AProcess.java:589)
	 *
	 * _appIcon = parseAppIcon(_appInfo);
	 */
	public ProcessItem(Context context, int pid, String pkgName) {
		this.context = context;
		this.pid = pid;
		this.pkgName = pkgName;
		this.iconUri = "package://" + pkgName;

//		this.appInfo = BatteryHelper.loadAppInfo(getContext(), pkgName);
//		this.appName = BatteryHelper.loadAppName(getContext(), appInfo);
		ApplicationInfo appInfo = BatteryHelper.loadAppInfo(getContext(), pkgName);
		this.appName = BatteryHelper.loadAppName(getContext(), appInfo);

		if(TextUtils.isEmpty(appName)) {
			this.appName = pkgName;
		}

		reloadRam();
////		reloadCpu();
////        mLastAppCpuTime = CpuUtil.getAppCpuTime(pid);
////        mLastTotalCpuTime = CpuUtil.getTotalCpuTime();
//		reloadBat(totalCpu, totalRam);
	}

	private Context getContext() {
		return context;
	}

	private void reloadRam() {
		this.ram = RamUtil.getProcessRam(getContext(), pid);
	}

//	private void reloadBat(long totalCpu, long totalRam) {
//		this.bat = BatteryUtil.getBatteryUsage(pid, totalCpu, ram, totalRam);
//	}
//
//	public ProcessItem(Context context, AndroidAppProcess process, long totalCpu, long totalRam) {
//		this(context, process.pid, process.getPackageName(), totalCpu, totalRam);
//	}

	public ProcessItem(Context context, AndroidAppProcess process) {
		this(context, process.pid, process.getPackageName());
	}

	public ProcessItem(Context context, ApplicationInfo info) {
		this(context, 0, info.packageName);
	}

//	public static ProcessItem newProcessItem(Context context, Object object, long totalCpu, long totalRam) {
//		ProcessItem res = null;
	public static ProcessItem newProcessItem(Context context, Object object) {
		ProcessItem res;

		if(object instanceof RunningAppProcessInfo) {
			RunningAppProcessInfo rapi = (RunningAppProcessInfo) object;
//			res = new ProcessItem(context, rapi.pid, rapi.pkgList[0], totalCpu, totalRam);
			res = new ProcessItem(context, rapi.pid, rapi.pkgList[0]);

		} else if(object instanceof AndroidAppProcess) {
			AndroidAppProcess aap = (AndroidAppProcess) object;
//			res = new ProcessItem(context, aap.pid, aap.getPackageName(), totalCpu, totalRam);
			res = new ProcessItem(context, aap.pid, aap.getPackageName());

		} else if(object instanceof ActivityManager.RunningServiceInfo) {
			ActivityManager.RunningServiceInfo rsi = (ActivityManager.RunningServiceInfo) object;
//			res = new ProcessItem(context, rsi.pid, rsi.service.getPackageName(), totalCpu, totalRam);
			res = new ProcessItem(context, rsi.pid, rsi.service.getPackageName());

		} else if (object instanceof ApplicationInfo) {
			ApplicationInfo info = (ApplicationInfo) object;

			res = new ProcessItem(context, 0, info.packageName);

		}else {
			throw new IllegalArgumentException("Argument object is neither "
					+ RunningAppProcessInfo.class.getSimpleName() + " or "
					+ AndroidAppProcess.class.getSimpleName());

		}

		return res;
	}

	public String getPkgName() {
		return pkgName;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

//	public float getCpu() {
//		return cpu;
//	}

	public long getRamUsage() {
		return ram;
	}

//	public float getBattery() {
//		return bat;
//	}

	public boolean isChecked() {
		return isChecked;
	}

	public void setChecked(boolean checked) {
		this.isChecked = checked;
	}

	public void toggleChecked() {
		setChecked(!isChecked);
	}

}
